﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CO2FixCalibration
{
    class ReadOutput
    {
        
        static string[] content;

        public enum OutputType
        {
             biomass,
             CAI,
             dipterocarp,
             agathis
        }

        static void Initialize()
        {
            OutputType Columns = new OutputType();
            foreach (OutputType type in Enum.GetValues(typeof(OutputType)))
            {
                //Columns.biomass = GetColumnNumber(type);
            }
        }

        public ReadOutput(string filename)
        {
            content = System.IO.File.ReadAllLines(filename);
        }

        
        static string[] GetColumn(int col)
        {
            string[] Column = new string[content.Length];
            for (int line =0; line< content.Length;line++)
            {
                Column[line] = content[line].Split('\t')[col];
                //Column[line] = content[line].Split((char[])null, StringSplitOptions.RemoveEmptyEntries)[col];
            }
            return Column;
        }
        bool ColumnContains(string[] column, string term)
        {
            foreach (string word in column)
            {
                if (word.Contains(term)) return true;
            }
            return false;
        }

        bool ColumnContains(string[] column, string[] terms)
        {
            foreach (string word in column)
            {
                if (ColumnContains(column, word) == false) return false;
            }
            return true;
        }

        static int GetColumnNumber(OutputType type)
        {
            for (int col = 0; col < content[0].Split('\t').Length; col++)
            {
                string[] column = GetColumn(col);

                if (column[1].Trim().Contains("Biomass"))
                {
                    if (type == OutputType.biomass)
                    {
                        if (column[2].Trim().Length == 0 && column[3].Trim().Length == 0 && column[4].Trim().Contains("dry weight"))
                        {
                            return col;
                        }
                    }
                    else if (type == OutputType.agathis)
                    {
                        if (column[1].Trim().Contains("Biomass") && column[2].Trim().Contains("Agathis") && column[3].Trim().Length == 0)
                        {
                            return col;
                        }
                    }
                    else if (type == OutputType.dipterocarp)
                    {
                        if (column[1].Trim().Contains("Biomass") && column[2].Contains("Dipterocarps") && column[3].Trim().Length == 0)
                        {
                            return col;
                        }
                    }
                    else if (type == OutputType.CAI)
                    {
                        if (column[1].Trim().Contains("Biomass") && column[2].Trim().Length == 0 && column[3].Contains("stems") && column[4].Contains("CAI"))
                        {
                            return col;
                        }
                    }
                    else throw new System.Exception("Cannot find column for "+ type.ToString());
                }
               
            
            }
            throw new System.Exception("Cannot find column for "+ type.ToString());
          

        }
        public DataSet Read(OutputType[] Types, string label)
        {
            List<DataSet> data = new List<DataSet>();
            foreach (OutputType type in Types)
            {
                data.Add(Read(type, label));
            }

            foreach (DataSet d in data)
            { 
                for (int y = data[0].FirstTime; y < data[0].LastTime; y++)
                {
                    if (d.Time[y] != data[0].Time[y]) throw new System.Exception("Incompatible datasets adding");
                }
            }

            DataSet sumdata = new DataSet("");
            foreach (int time in data[0].Time)
            {
                double sum = 0;
                foreach (DataSet d in data)
                {
                    sum += d.GetValueAtTime(time);
                }
                sumdata.Add(time, sum);
            }
            return sumdata;
        }
        public DataSet Read(OutputType Type, string label)
        {
            DataSet d = new DataSet(label);
            int col = GetColumnNumber(Type);

            for (int r = 6; r < content.Length; r++)
            {
                int year = int.Parse( content[r].Split('\t')[0]);
                double Value = double.Parse(content[r].Split('\t')[col]);
                d.Add(year, Value);
            }


            return d;
        }
    }
}
